package com.jydd.investtheretreatoldhouse;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;

import static android.provider.Settings.EXTRA_APP_PACKAGE;
import static android.provider.Settings.EXTRA_CHANNEL_ID;

public class MainActivity extends AppCompatActivity {
    private TextView tvShowDeviceInfo;
    private AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvShowDeviceInfo = findViewById(R.id.tvShowDeviceInfo);
        alertDialog = new AlertDialog.Builder(this)
                .setTitle("提醒")
                .setMessage("请打开通知权限")
                .setCancelable(false)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 手动打开通知权限
                        jumpToOpenTheNotification();
                    }
                }).create();
    }

    private void jumpToOpenTheNotification() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
        // 这种方案适用于API 26, 即8.0（含8.0）以上可以用
        intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());
        intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);
        // 这种方案适用于API21-25, 即5.0-7.1之间的版本可用
        intent.putExtra("app_package", getPackageName());
        intent.putExtra("app_uid", getApplicationInfo().uid);
        // 小米6 -MIUI9.6-8.0.0系统，是个特例，通知设置界面只能控制"允许使用通知圆点"——然而这个玩意并没有卵用，我想对雷布斯说：I'm not ok!!!
        //  if ("MI 6".equals(Build.MODEL)) {
        //      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        //      Uri uri = Uri.fromParts("package", getPackageName(), null);
        //      intent.setData(uri);
        //      // intent.setAction("com.android.settings/.SubSettings");
        //  }
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 检查通知权限是否开启
        checkNotifySetting();
    }

    private void checkNotifySetting() {
        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        // areNotificationsEnabled方法的有效性官方最低支持到API19，低于19的仍可调用此方法，不过只会返回true，即默认用户已经开启通知
        boolean isOpen = manager.areNotificationsEnabled();
        if(isOpen){
            tvShowDeviceInfo.setText("通知权限已经被打开"+
                    "\n手机型号:" + Build.MODEL +
                    "\nSDK版本:" + Build.VERSION.SDK +
                    "\n系统版本:" + Build.VERSION.RELEASE +
                    "\n软件包名:" + getPackageName());
        } else {
            tvShowDeviceInfo.setText("通知权限未打开");
            // 手动去开启通知权限
            alertDialog.show();
        }
    }
}
